package com.haofengsoft.scce.adapter.optional;

import java.util.List;

import com.haofengsoft.scce.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class OptionalAdapter extends BaseAdapter {

	Context context;
	List<OptionalModel> optionalModelList;

	public OptionalAdapter(Context context, List<OptionalModel> optionalModelList) {
		this.context = context;
		this.optionalModelList = optionalModelList;
	}

	@Override
	public int getCount() {
		return this.optionalModelList.size();
	}

	@Override
	public Object getItem(int position) {
		return this.optionalModelList.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	public void addOptionalList(List<OptionalModel> optionalModelList) {
		this.optionalModelList.addAll(optionalModelList);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		if (convertView == null) {
			view = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.optional_item, parent, false);
		} else {
			view = (ViewGroup) convertView;
		}

		TextView optionalNameTv = (TextView) view.findViewById(R.id.optional_stamp_name_tv);
		TextView optionalNumberTv = (TextView) view.findViewById(R.id.optional_stamp_number_tv);
		TextView optionalPriceTv = (TextView) view.findViewById(R.id.optional_stamp_price_tv);
		TextView optionalUpDownTv = (TextView) view.findViewById(R.id.optional_stamp_up_down_tv);
		OptionalModel optionalModel = (OptionalModel) getItem(position);

		optionalNameTv.setText(optionalModel.getName());
		optionalNumberTv.setText(optionalModel.getNumber());
		optionalPriceTv.setText(optionalModel.getPrice());
		optionalUpDownTv.setText(optionalModel.getUpAndDown());

		if (optionalModel.isUpOrDown()) {
			optionalUpDownTv.setBackgroundColor(context.getResources().getColor(R.color.common_rise));
		} else {
			optionalUpDownTv.setBackgroundColor(context.getResources().getColor(R.color.common_down));
		}
		return view;
	}
}
